﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CinemaBookingUI.WCFReference;

namespace CinemaBookingUI
{
    public partial class MainScreen : Form
    {
        private WCFReference.IWcfCinemaService  wcf = SingletonWCF.Instance.WCFNetTcp();
        private bool isOpen = false;

        public MainScreen()
        {
            InitializeComponent();
            try
            {
                int nowYear = DateTime.Today.Year;
                int nowMonth = DateTime.Today.Month;
                int nowDate = DateTime.Today.Day;
                dpdMovie.DataSource = SortList();

                dpdDate.DataSource = DatesOneMonth(nowYear, nowMonth, nowDate);
                lblSelectedDate.Text = dpdDate.SelectedItem.ToString();
                ShowMovies();
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }

        }

        private void ShowMovies()
        {
            if (dpdMovie.SelectedValue.ToString().Equals("Vælg Film"))
            {
                //This function is supposed to be the main function but until more data
                //is added use the ReadAll.
                dgvIndex.DataSource = wcf.DateMovie(dpdDate.SelectedItem.ToString());
                //dgvIndex.DataSource = wcf.ReadAllMovies();
                
            }
            else
            {

                dgvIndex.DataSource = wcf.ReadMovie(dpdMovie.SelectedItem.ToString());
            }
        }

        private List<string> SortList()
        {
            List<string> sortedList = new List<string>();
            sortedList.Add("Vælg Film");
            foreach (CinemaBookingUI.WCFReference.Movie mov in wcf.ReadAllMovies())
            {
                sortedList.Add(mov.Title);
            }
            return sortedList;
        }

        private List<string> DatesOneMonth(int nowYear, int nowMonth, int nowDate)
        {
            var dates = new List<string>();
            for (var date = new DateTime(nowYear, nowMonth, nowDate); date.Month == nowMonth; date = date.AddDays(1))
            {
                if (date.Day >= nowDate)
                {
                    dates.Add(date.ToShortDateString());
                }

            }
            return dates;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GoToCustomerSearch();
        }

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            bool Used = true;
            if (!isOpen)
            {
                if (e.KeyCode == Keys.Enter && Used)
                {
                    GoToCustomerSearch();
                    Used = false;
                }
                else
                {
                    Used = true;
                }
            }
        }

        private void dgvIndex_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int movieId = 0;
            int showId = 0;

            foreach (DataGridViewRow row in dgvIndex.SelectedRows)
            {
                if (row.Index == e.RowIndex)
                {
                    movieId = Convert.ToInt32(row.Cells["Id"].Value.ToString());
                    showId = Convert.ToInt32(row.Cells["ShowTimeID"].Value.ToString());
                }
            }

            CinemaRoom room = new CinemaRoom(wcf.ReadMovieById(movieId), wcf.ReadShowTime(showId));
            try
            {
                if (showId != 0)
                {
                    room.Show();
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("Der er ingen forestillinger for den given film\r\npå det given tidspunkt.");
            }
            
        }

        private void GoToCustomerSearch()
        {
            string txt = txtSearch.Text.Trim();
            int phone;
            bool isPhone = int.TryParse(txt, out phone);
            if (isPhone)
            {
                CustomerSearch cus = new CustomerSearch(txtSearch.Text);
                cus.Show();
            }
            else
            {
                string mes = "Du skal indtaste et telefon nummer i søgefeltet.\r\nTryk på OK knappen eller tryk på Escape tasten.";
                string cap = "Fejl";
                MessageBoxButtons but = MessageBoxButtons.OK;
                DialogResult result;
                isOpen = true;
                result = MessageBox.Show(this, mes, cap, but);

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    isOpen = false;
                }
            }
        }

        private void dpdDate_SelectionChangeCommitted(object sender, EventArgs e)
        {
            lblSelectedDate.Text = dpdDate.SelectedItem.ToString();
            dgvIndex.DataSource = wcf.DateMovie(dpdDate.SelectedItem.ToString());
        }

        private void dpdMovie_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ShowMovies();
        }
    }
}